package com.huabo.system.controller;

import java.math.BigDecimal;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.huabo.system.config.DateBaseConfig;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hbfk.util.JsonBean;
import com.hbfk.util.ResponseFormat;
import com.huabo.system.oracle.entity.TblSystemRight;
import com.huabo.system.service.TblManageRightService;
import com.huabo.system.service.TblManageScreenRightService;
import com.huabo.system.service.TblSystemRightService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
@RequestMapping("/right")
@Api(value="获取权限信息接口",tags={"获取权限接口"})
public class RightController {
	
	@Resource
	private TblManageRightService tblManageRightService;
	
	@Resource
	private TblSystemRightService tblSystemRightService;

	@Resource
    private TblManageScreenRightService tblManageScreenRightService;
	
	@RequestMapping(value = "/screenRight/saveRightInfo", produces = "application/json; charset=utf-8", method = {RequestMethod.POST})
    @ApiParam("报表授权，保存角色授权信息")
    public String screenRight_saveRightInfo(HttpServletRequest reques,
    		 @ApiParam(name="roleId",value="角色ID主键",required=true)@RequestParam(value = "roleId", required = true) String roleId,
    		 @ApiParam(name="rightIds",value="权限主键集合，用,分割 示例 1,2,3,4",required=true) @RequestParam(value = "rightIds", required = true) String rightIds) throws Exception {
        if (StringUtils.isNotBlank(roleId) && StringUtils.isNotBlank(rightIds)) {
           this.tblManageScreenRightService.grantScreenRightToRole(roleId, rightIds);
           return JsonBean.success();
        }
        return JsonBean.error();
    }
	
	@RequestMapping(value = "/screenRight/getAllRightList", produces = "application/json; charset=utf-8", method = {RequestMethod.GET})
    @ApiParam("报表授权，授权页面获取权所有报表权限")
    public JsonBean screenRight_getAllRightList(HttpServletRequest reques,
    		 @ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
    		 @ApiParam(name="rightId",value="权限主键",required=false) @RequestParam(value = "rightId", required = false) Integer rightId,
    		 @ApiParam(name="roleId",value="授权角色主键",required=false) @RequestParam(value = "roleId", required = true) Integer roleId) throws Exception {
           JsonBean jsonBean = null;
			try {
				jsonBean =  this.tblManageScreenRightService.getAllRightList(token, rightId, roleId);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return jsonBean;
    }
	
	
	@RequestMapping(value = "/screenRight/getRoleRightList", produces = "application/json; charset=utf-8", method = {RequestMethod.POST})
    @ApiParam("报表授权，根据角色获取权限列表")
    public JsonBean screenRight_getRoleRightList(HttpServletRequest reques,
    		 @ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
    		 @ApiParam(name="rightId",value="权限主键",required=false) @RequestParam(value = "rightId", required = false) Integer rightId) throws Exception {
           JsonBean jsonBean = null;
			try {
				jsonBean =  this.tblManageScreenRightService.getScreenRoleRightList(token, rightId);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return jsonBean;
    }
	
	@RequestMapping(value = "/getRoleRigetListByType", produces = "application/json; charset=utf-8", method = {RequestMethod.GET})
	@ApiOperation(value = "角色权限获取-根据类型，模块类型获取该节点下的所有权限节点")
	public JsonBean getRoleRigetListByType(HttpServletRequest request,
			 @ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
			 @ApiParam(name="type",value="权限类型 为空默认返回0和1树形菜单",required=false)@RequestParam(value="type",required=false)Integer type,
			 @ApiParam(name="moduleType",value="模块类型",required=false)@RequestParam(value="moduleType",required=false)String moduleType)throws Exception{
			JsonBean jsonBean = null;
			try {
				jsonBean = this.tblSystemRightService.findRoleRigetListByType(token,type,moduleType);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return jsonBean;
	 }
	
	@RequestMapping(value = "/modifySystemRightVisible", produces = "application/json; charset=utf-8", method = {RequestMethod.GET})
	@ApiOperation(value = "权限管理-根据权限ID修改权限菜单的显示隐藏")
	public JsonBean modifySystemRightVisible(HttpServletRequest request,
			 @ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
			 @ApiParam(name="rightId",value="权限ID",required=true)@RequestParam(value="rightId",required=true)Integer rightId,
			 @ApiParam(name="visible",value="是否隐藏 1正常显示  0隐藏",required=true)@RequestParam(value="visible",required=true)Integer visible)throws Exception{
			JsonBean jsonBean = null;
			try {
				jsonBean = this.tblSystemRightService.modifySystemRightVisible(token,rightId,visible);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return jsonBean;
	 }
	
	@RequestMapping(value = "/roleRightList", produces = "application/json; charset=utf-8", method = {RequestMethod.GET})
	@ApiOperation(value = "角色权限获取-根据rightId，模块类型获取该节点下的所有权限节点")
	public JsonBean childrenRightListAllRole(HttpServletRequest request,
			 @ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
			 @ApiParam(name="rightId",value="父级权限Id 默认0",required=false)@RequestParam(value="rightId",required=false)Integer rightId,
			 @ApiParam(name="moduleType",value="模块类型",required=false)@RequestParam(value="moduleType",required=false)String moduleType)throws Exception{
			JsonBean jsonBean = null;
			try {
				jsonBean = this.tblSystemRightService.findRightListByRole(token,rightId,moduleType);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return jsonBean;
	 }
	
	@RequestMapping(value = "/childrenRoleRightList", produces = "application/json; charset=utf-8", method = {RequestMethod.GET})
	 @ApiOperation(value = "角色权限获取-根据rightId，模块类型获取该节点下的子级权限")
	 public JsonBean childrenRoleRightList(HttpServletRequest request,
			 @ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
			 @ApiParam(name="rightId",value="父级权限Id 默认0",required=false)@RequestParam(value="rightId",required=false)Integer rightId,
			 @ApiParam(name="moduleType",value="模块类型",required=false)@RequestParam(value="moduleType",required=false)String moduleType)throws Exception{
		 JsonBean jsonBean = null;
			try {
				if(rightId == null) {
					rightId = 0;
				}
				jsonBean = this.tblSystemRightService.findRightListByRole(token,rightId,moduleType);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return jsonBean;
	 }
	
	@RequestMapping(value = "/getRoleRightList", produces = "application/json; charset=utf-8", method = {RequestMethod.GET})
	 @ApiOperation(value = "角色管理-权限设定-获取授权权限树形列表")
	 public JsonBean getRoleRightList(HttpServletRequest request,
			 @ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
			 @ApiParam(name="rightId",value="权限Id 默认0",required=false)Integer rightId,
			 @ApiParam(name="roleId",value="授权角色Id",required=true)Integer roleId,
			 @ApiParam(name="moduleType",value="模块类型",required=false)@RequestParam(value="moduleType",required=false)String moduleType)throws Exception{
			JsonBean jsonBean = null;
			try {
//				if(rightId == null) {
//					rightId = 0;
//				}
				rightId = 0;
				jsonBean = this.tblSystemRightService.findAllRightListByCompanyToGrant(token,rightId,roleId,moduleType);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return jsonBean;
	 }
	
	 @RequestMapping(value = "/grantRoleRight", produces = "application/json; charset=utf-8", method = {RequestMethod.POST})
	 @ApiOperation(value = "角色管理权限设定-保存角色授权信息")
	 public JsonBean grantRoleRight(HttpServletRequest request,@ApiParam(name="rightIds",value="权限多个主键，用,号分割 ，示例 1,2,3",required=true)String rightIds,
			 @ApiParam(name="roleId",value="角色主键",required=true)Integer roleId,
			 @ApiParam(name="moduleType",value="模块类型",required=false)@RequestParam(value="moduleType",required=false)String moduleType,
			 @ApiParam(name="token",value="登录用户token",required=false) @RequestHeader("token")String token)throws Exception{
		 JsonBean jsonBean = null;
			try {
				jsonBean = this.tblSystemRightService.grantRoleRight(rightIds,token,roleId,moduleType);
			} catch (Exception e) {
				ResponseFormat.retParam(1,1000,e.getMessage());
			}
			return jsonBean;
	 }
	 
	 @RequestMapping(value = "/rightManageRightList", produces = "application/json; charset=utf-8", method = {RequestMethod.GET})
	 @ApiOperation(value = "权限管理-列表页获取所有权限")
	 public JsonBean rightManageRightList(HttpServletRequest request,
			 @ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
			 @ApiParam(name="right",value="系统权限实体",required=false)TblSystemRight right,
			 @ApiParam(name="judge",value="是否传入筛选条件 0不带入 1带入",required=false)@RequestParam(value="judge",required=false)Integer judge)throws Exception{
		 JsonBean jsonBean = null;
			try {
				jsonBean = this.tblSystemRightService.findAllRightListcf(token,right,judge);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return jsonBean;
	 }
	 
	 
	 @RequestMapping(value = "/saveManageRight", produces = "application/json; charset=utf-8", method = {RequestMethod.POST})
	 @ApiOperation(value = "权限管理 - 新增、修改权限接口")
	 public JsonBean saveManageRight(HttpServletRequest request,@ApiParam(name="right",value="权限Entity",required=true)TblSystemRight right,
			 @ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token)throws Exception{
		 JsonBean jsonBean = null;
			try {
				jsonBean = this.tblSystemRightService.saveManageRight(right,token);
			} catch (Exception e) {
				ResponseFormat.retParam(1,1000,e.getMessage());
			}
			return jsonBean;
	 }
	 
	 
	 @RequestMapping(value = "/findRightEntityById", produces = "application/json; charset=utf-8", method = {RequestMethod.GET})
	 @ApiOperation(value = "权限管理-根据权限ID获取权限信息")
	 public JsonBean findRightEntityById(HttpServletRequest request,
			 @ApiParam(name="id",value="权限主键Id",required=true)@RequestParam(value="id",required=true)BigDecimal id)throws Exception{
		 JsonBean jsonBean = null;
			try {
				jsonBean = this.tblSystemRightService.findRightEntityById(id);
			} catch (Exception e) {
				ResponseFormat.retParam(1,1000,e.getMessage());
			}
			return jsonBean;
	 }
	 
	 @RequestMapping(value = "/removeManageRight", produces = "application/json; charset=utf-8", method = {RequestMethod.POST})
	 @ApiOperation(value = "权限管理-删除权限接口")
	 public JsonBean removeManageRight(HttpServletRequest request, 
			 @ApiParam(name="rightId",value="权限主键Id",required=true)@RequestParam(value="rightId",required=true)BigDecimal rightId)throws Exception{
		 JsonBean jsonBean = null;
			try {
				jsonBean = this.tblSystemRightService.removeManageRight(rightId);
			} catch (Exception e) {
				ResponseFormat.retParam(1,1000,e.getMessage());
			}
			return jsonBean;
	 }
	 
	 
	 
	 
	 @RequestMapping(value = "/getCompanyRightList", produces = "application/json; charset=utf-8", method = {RequestMethod.GET})
	 @ApiOperation(value = "公司管理-菜单设定-获取权限树形列表")
	 public JsonBean getCompanyRightList(HttpServletRequest request,
			 @ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
			 @ApiParam(name="rightId",value="权限Id 默认0",required=false)Integer rightId,
			 @ApiParam(name="orgId",value="授权公司主键",required=true)Integer orgId,
			 @ApiParam(name="moduleType",value="模块类型",required=false)@RequestParam(value="moduleType",required=false)String moduleType
			 )throws Exception{
			JsonBean jsonBean = null;
			try {
				if(rightId == null) {
					rightId = 0;
				}
				jsonBean = this.tblSystemRightService.findAllRightListByCompany(token,rightId,orgId,moduleType);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return jsonBean;
	 }
	 
	 @RequestMapping(value = "/grantCompanyRight", produces = "application/json; charset=utf-8", method = {RequestMethod.POST})
	 @ApiOperation(value = "公司管理-菜单设置-保存公司授权信息")
	 public JsonBean grantCompanyRight(HttpServletRequest request,@ApiParam(name="rightIds",value="权限多个主键，用,号分割 ，示例 1,2,3",required=true)String rightIds,
			 @ApiParam(name="orgId",value="公司主键",required=true)String orgId,
			 @ApiParam(name="token",value="登录用户token",required=false) @RequestHeader("token")String token,
			 @ApiParam(name="moduleType",value="模块类型",required=true)@RequestParam(value="moduleType",required=true)String moduleType)throws Exception{
		 JsonBean jsonBean = null;
			try {
				if(DateBaseConfig.DATABASETYPE.equals("Oracle")){
					jsonBean = this.tblSystemRightService.grantCompanyRight(rightIds,token,orgId,moduleType);
				}else{
					jsonBean = this.tblSystemRightService.grantCompanyRightMySql(rightIds,token,orgId,moduleType);
				}

			} catch (Exception e) {
				ResponseFormat.retParam(1,1000,e.getMessage());
			}
			return jsonBean;
	 }
	 
	 
	 @RequestMapping(value = "/getCompanySettingRightList", produces = "application/json; charset=utf-8", method = {RequestMethod.GET})
	 @ApiOperation(value = "系统配置-菜单设置-左侧获取权限树形列表")
	 public JsonBean getCompanySettingRightList(HttpServletRequest request,
			 @ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
			 @ApiParam(name="rightId",value="权限Id 默认0",required=false)Integer rightId,
			 @ApiParam(name="moduleType",value="模块类型",required=false)@RequestParam(value="moduleType",required=false)String moduleType)throws Exception{
			JsonBean jsonBean = null;
			try {
				if(rightId == null) {
					rightId = 0;
				}
				jsonBean = this.tblSystemRightService.getCompanySettingRightList(token,rightId,moduleType);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return jsonBean;
	 }
	 
	 @RequestMapping(value = "/getCompanySettingRightListInfo", produces = "application/json; charset=utf-8", method = {RequestMethod.GET})
	 @ApiOperation(value = "系统配置-菜单设置-右侧获取权限列表信息")
	 public JsonBean getCompanySettingRightListInfo(HttpServletRequest request,
			 @ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
			 @ApiParam(name="rightId",value="权限Id 默认0",required=false)Integer rightId,
			 @ApiParam(name="moduleType",value="模块类型",required=false)@RequestParam(value="moduleType",required=false)String moduleType)throws Exception{
			JsonBean jsonBean = null;
			try {
				if(rightId == null) {
					rightId = 0;
				}
				jsonBean = this.tblSystemRightService.getCompanySettingRightListInfo(token,rightId,moduleType);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return jsonBean;
	 }
	 
	 @RequestMapping(value = "/findSystemRightSettingById", produces = "application/json; charset=utf-8", method = {RequestMethod.GET})
	 @ApiOperation(value = "系统配置-菜单设置-根据Id获取权限信息")
	 public JsonBean findSystemRightSettingById(HttpServletRequest request,
			 @ApiParam(name="id",value="权限主键Id",required=true)@RequestParam(value="id",required=true)BigDecimal id)throws Exception{
		 JsonBean jsonBean = null;
			try {
				jsonBean = this.tblSystemRightService.findSystemRightSettingById(id);
			} catch (Exception e) {
				ResponseFormat.retParam(1,1000,e.getMessage());
			}
			return jsonBean;
	 }
	 
	 
}
